package com.bdsy.business.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.bdsy.business.model.ShopIdentInfo;
import com.bdsy.business.model.ShopImage;
import com.bdsy.business.model.ShopSellInfo;
import com.bdsy.business.service.ShopIdentInfoService;
import com.bdsy.business.service.ShopImageService;
import com.bdsy.business.service.ShopSellInfoService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.DateUtil;
import com.bdsy.common.utils.IcdnUtils;
import com.bdsy.common.utils.QRUtils;
import com.bdsy.common.utils.Result;

/**
 * 鉴定信息
 * @author MING
 * @date 2018年1月12日 下午5:39:06
 */
@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class ShopIdentInfoController {

	@Autowired
	ShopIdentInfoService shopIdentInfoService;

	@Autowired
	ShopImageService shopImageService;
	
	@Autowired
	ShopSellInfoService shopSellInfoService;
	
	@RequestMapping(value = "idenInfo")
	public String idenInfo() {
		return "admin/business/idenInfo";
	}
	
	
	/**
	 * 根据商品Id查询鉴定信息
	 */
	@Json
	@RequestMapping(value = "shopIdentInfoList")
	public Object shopIdentInfoList(int shopId) {
		
		Map<String,Object> map = new LinkedHashMap<>();
		
		@SuppressWarnings("rawtypes")
		List<Map> shopIdentInfoList = shopIdentInfoService.shopIdentInfoList(shopId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", shopIdentInfoList);
		return map;
	}
	
	
	/**
	 * 查询鉴定信息
	 */
	@Json
	@RequestMapping(value = "shopIdentInfoLists")
	public Object shopIdentInfoLists() {
		Map<String,Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> shopIdentInfoList = shopIdentInfoService.shopIdentInfoLists();
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", shopIdentInfoList);
		return map;
	}
	
	
	/**
	 * 添加鉴定信息
	 */
	@Json
	@RequestMapping(value = "addShopIdentInfo")
	public Object addShopIdentInfo(MultipartFile file,ShopIdentInfo shopIdentInfo,ShopImage shopImage) {
		
		int number = (int)((Math.random()*9+1)*10000000);
		shopIdentInfo.setShopId(number);
		shopIdentInfo.setIdentTime(DateUtil.getCurrentDate());
		//添加鉴定信息
		boolean result = shopIdentInfoService.insert(file,shopIdentInfo);
		
		//添加艺术品信息   商品ID  二维码 
		
		String icdn = IcdnUtils.getIcdns();
//		  String path = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\adicms_image\\qrimages";
		  String path = "/usr/local/tomcat/webapps/adicms_image/qrimages";
		  String name = new Random().nextInt(99999999)+".jpg"; 
		
		  // 依次为内容(不支持中文),宽,长,中间图标路径,储存路径  
//		  QRUtils.encode(icdn, 512, 512,path+"\\"+"logo.jpg", path+"\\"+name);
		  QRUtils.encode(icdn, 512, 512,path+"/logo.jpg", path+"/"+name);
		  System.out.println("生成成功");
			 shopImage.setQrcodeUrl("/qrimages/"+name);
			 shopImage.setShopId(number);
		  // 添加图片
		 shopImageService.insert(shopImage);
		 // 添加 icdn  商品id
		 ShopSellInfo shopSellInfo = new ShopSellInfo();
		 shopSellInfo.setShopId(number);
		 shopSellInfo.setIcdn(icdn);
		 shopSellInfo.setIdentStatus(1);
		 shopSellInfoService.add(shopSellInfo);
		if (result) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}
	
}
